﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;



namespace client
{
    public partial class Form1 : Form
    {
        private byte[] data = new byte[1024];
        private Socket client;


        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] message = Encoding.ASCII.GetBytes(textBox3.Text);
                client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(onDatasent), null);
            }
            catch (SocketException)
            {
                CloseConnection();
            }
        }



        void onDatasent(IAsyncResult result)
        {
            try
            {
                int sent = client.EndSend(result);
                client.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(onDatasent), null);
            }
            catch (SocketException)
            {


                CloseConnection();

            }

        }
        private void button3_Click(object sender, EventArgs e)
        {
            CloseConnection();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                button1.Enabled = true;
                button3.Enabled = true;
                button2.Enabled = false;

                textBox4.Text = "Connecting to server .....";

                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                int port;
                port = int.Parse(textBox2.Text);
                IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(textBox1.Text), port);
                client.BeginConnect(remoteEP, new AsyncCallback(OnConnected), null);
            }
            catch
            {
                CloseConnection();
            }
        }



        void OnConnected(IAsyncResult result)
        {
            try
            {
                client.EndConnect(result);
                textBox4.Text = "Connected to:  = " + client.RemoteEndPoint;
                client.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null);
            }
            catch
            {
                CloseConnection();
            }
        }
        void OnDataReceived(IAsyncResult result)
        {
            try
            {
                int receive = client.EndReceive(result);
                string message = Encoding.ASCII.GetString(data, 0, receive);
                listBox1.Items.Add(message);
            }
            catch (Exception)
            {
                CloseConnection();
            }

        }
        public void CloseConnection()
        {
            client.Close();
            textBox4.Text = "Disconnected  ...";
            button1.Enabled = true;
            button3.Enabled = false;
            button2.Enabled = false;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

